<?php
require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_ticket.php";
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_movie.php";
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_seat.php";
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_seat_template.php";
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_cinema.php";
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_sql_convert.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_date.php';


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of api_security
 *
 * @author Thien
 */
class api_tickets {
    
    public static function save_ticket($ticket_id, $name, $show_date, 
                $time_start, $duration, $movie_id, $cinema_id, $price) {
        $dao_ticket = new dao_ticket();
        $dao_seat_template = new dao_seat_template();
        $count_seats = $dao_seat_template->count_all();
        $inserted_id = $dao_ticket->save($ticket_id, $name, lib_date::convert_to_sql_date($show_date, "d/m/Y"), 
            lib_date::convert_to_sql_time($time_start, "H:i"), $duration, $movie_id, $cinema_id, $price,
                $count_seats, $count_seats);
        if ($ticket_id == 0) {
            $dao_seat = new dao_seat();
            
            $template = $dao_seat_template->get_all();
            foreach ($template as $item) {
                $is_wheelchair = 0;
                if ($item['seat_status_id'] == 3) {
                    $is_wheelchair = 1;
                }
                $dao_seat->save(0, $item['row_pos'], 
                        $item['column_pos'], $inserted_id, $item['seat_status_id'],
                        $is_wheelchair);
            }
        }
        return TRUE;
    }
    
    public static function delete_ticket($ticket_id) {
        $dao_ticket = new dao_ticket();
        return $dao_ticket->delete($ticket_id);
    }
    
    public static function get_user_cinema_email($ticket_id) {
        $dao_ticket = new dao_ticket();
        $ticket = $dao_ticket->get_by_id($ticket_id);
        $dao_user = new dao_user();
        $user = $dao_user->get_cinema_user($ticket['cinema_id']);
        return $user['email'];
    }
    
    public static function get_ticket_by_ticket_booking($city_id, $cinema_id, $movie_id, $show_date, $time_start) {
        $dao_ticket = new dao_ticket();
        $ticket = $dao_ticket->get_all_by_filters(0, $city_id, $cinema_id, $movie_id, $show_date, $time_start);
        if (!empty($ticket) && $ticket > 0) {
            return $ticket[0];
        }
    }
    
    public static function get_all_tickets_by_ticket_booking($ticket_quantity, $city_id, $movie_id, $show_date) {
        $dao_ticket = new dao_ticket();
        $tickets = $dao_ticket->get_all_by_filters($ticket_quantity, $city_id, 0, $movie_id, $show_date, "");
//        var_dump($tickets);
//        exit()
        $list = array();
        if (!empty($tickets) && count($tickets) > 0) {
            $list[0] = $tickets[0];
            for ($index = 1; $index < count($tickets); $index++) {
                if ($tickets[$index]['cinema_name'] !== $tickets[$index - 1]['cinema_name']
                        || $tickets[$index]['time_start'] !== $tickets[$index - 1]['time_start']) {
                    $list[count($list)] = $tickets[$index];
                }
            }
        }
        return $list;
    }
    
    public static function get_ticket_by_id($ticket_id) {
        $dao_ticket = new dao_ticket();
        return $dao_ticket->get_by_id($ticket_id);
    }
    
    public static function get_all_tickets() {
        $dao_ticket = new dao_ticket();
        return $dao_ticket->get_all();
    }
    
    public static function get_all_tickets_live() {
        $dao_ticket = new dao_ticket();
        $list = $dao_ticket->get_all_live();
        for ($index = 0; $index < count($list); $index++) {
            $list[$index]['out_of_date'] = "-";
        }
        return $list;
    }
    
    public static function get_all_tickets_dead() {
        $dao_ticket = new dao_ticket();
        $list = $dao_ticket->get_all_dead();
        for ($index = 0; $index < count($list); $index++) {
            $list[$index]['out_of_date'] = "OUT OF DATE!";
        }
        return $list;
    }
    
    public static function get_all_live_first_dead_later() {
        $result = array_merge((self::get_all_tickets_live()), self::get_all_tickets_dead());
        return $result;
    }
    
    public static function get_all_movies() {
        $dao_movie = new dao_movie();
        return $dao_movie->get_all();
    }
    
    public static function get_all_cinemas() {
        $dao_cinema = new dao_cinema();
        return $dao_cinema->get_all();
    }
    
    public static function get_all_tickets_with_now_showing() {
        $dao_ticket = new dao_ticket();
        $status_id = 2; //for now showing
        return $dao_ticket->get_all_by_status_id($status_id);
    }
    
    public static function get_all_tickets_with_comming_soon() {
        $dao_ticket = new dao_ticket();
        $status_id = 1; //for comming soon
        return $dao_ticket->get_all_by_status_id($status_id);
    }
    
    public static function get_all_ticket_statuses() {
        $dao_ticket_status = new dao_ticket_status();
        return $dao_ticket_status->get_all();
    }
    
    public static function get_default_movie() {
        $dao_movie = new dao_movie();
        $movie = $dao_movie->get_first();
        if (!empty($movie['title'])) {
            return $movie['title'];
        }
        return "";
    }
    
    public static function get_default_cinema() {
        $dao_cinema = new dao_cinema();
        $cinema = $dao_cinema->get_first();
        if (!empty($cinema['name'])) {
            return $cinema['name'];
        }
        return "";
    }
    
    public static function get_movie_id_from_title($title) {
        $dao_movie = new dao_movie();
        $movie = $dao_movie->get_id_by_title($title);
        return $movie['id'];
    }
    
    public static function get_cinema_id_from_name($name) {
        $dao_cinema = new dao_cinema();
        $cinema = $dao_cinema->get_id_by_name($name);
        return $cinema['id'];
    }
    
    public static function validate_ticket_fields($name, $show_date, 
                $time_start, $duration, $movie, $cinema, $price) {
        $error_show = "";
        if (empty($name)) {
            $error_show .= "<li>Please fill Name.</li>";
        }
        if (empty($movie)) {
            $error_show .= "<li>Please choose a Movie.</li>";
        }
        if (empty($cinema)) {
            $error_show .= "<li>Please choose a Cinema.</li>";
        }
        if (empty($show_date)) {
            $error_show .= "<li>Please fill Show Date.</li>";
        }
        if (empty($time_start)) {
            $error_show .= "<li>Please fill Time Start.</li>";
        }
        if (empty($duration)) {
            $error_show .= "<li>Please fill Duration.</li>";
        }
        if (empty($price)) {
            $error_show .= "<li>Please fill Price.</li>";
        }
        return $error_show;
    }
    
}
